package me.leiusual.sharing.ui.cart.adapter

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_cart_order_child.*
import me.leiusual.sharing.api.model.cartdetail.CartX
import me.leiusual.sharing.util.extension.setImageUrl
import me.leiusual.sharing.util.extension.spToPx
import me.leiusual.sharing.util.extension.toSpanned

/**
 * author: leiusual
 * create on: 2019-07-31
 * description:
 */
class CartOrderChildViewHolder(
    override val containerView: View
) : RecyclerView.ViewHolder(containerView), LayoutContainer {

  fun bind(cartX: CartX) {

    // 图片
    image_view.setImageUrl(cartX.productImage)

    // 名称
    text_view_product_name.text = cartX.modelName

    // 价格
    val size = 10f.spToPx(containerView.context)
    val builder = StringBuilder()
    builder.append("""<font color="red">""")
    cartX.priceVos.forEach {
      when (it.coinType) {
        "RMB" -> builder.append("""<size_$size>￥</size_$size>""")
        else -> builder.append("""<size_$size>分</size_$size>""")
      }
      builder.append("""${it.pointCoinString}  """)
    }
    if (cartX.independentFreight > 0) {
      builder.append("""<size_$size>运</size_$size>""")
      builder.append(cartX.independentFreight)
    }
    builder.append("""</font>""")
    text_view_price.text = builder.toString().toSpanned()

    // 数量
    text_view_quantity.text = "数量${cartX.quantity}"
  }
}